<?php
class CreateprojectForm extends Zend_Form
{
    public function init()
    {
		$idProject = new Zend_Form_Element_Hidden('idProject');
		$idPerson = new Zend_Form_Element_Hidden('Person_idPerson');

		// Retrieve the user id from the session
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		$userid = $data->idPerson;
		
		// prepare the where statement
		$where = Zend_Db_Table_Abstract::getDefaultAdapter()
					->quoteInto("Person_idPerson = ?", $userid);
		
        $name = $this->createElement('text','name');
        $name->setLabel('Name : *')
                    ->setRequired(true)
                    ->addValidator(new Zend_Validate_Regex("/^\w[\w ]*$/"))
                    ->addValidator(new Zend_Validate_StringLength(0, 45))
                    // Check if the current username has not already a project with the same name
                    ->addValidator(new Zend_Validate_Db_NoRecordExists('Project', 'name', $where));
                    
        $enterprise = $this->createElement('text','enterprise');
        $enterprise->setLabel('Enterprise :')
        	->addValidator(new Zend_Validate_StringLength(0, 45));
                
        $website = $this->createElement('text','website');
        $website->setLabel('Website :')
        	->addValidator(new Zend_Validate_StringLength(0, 45));
        
        $startDate = $this->createElement('text','startDate');
        $startDate->setLabel('Start date :')
			->addValidator(new Zend_Validate_Date('dd-MM-YYYY'));
                
        $endDate = $this->createElement('text','endDate');
        $endDate->setLabel('End date :')
        	->addValidator(new Zend_Validate_Date('dd-MM-YYYY'))
/*         	->addValidator(new Perso_Validate_DateGreaterThan('startDate')) */;
                
        $note = $this->createElement('textarea','note');
        $note->setLabel('Note :')
        	->setAttribs(array(
                'cols' => 70,
                'rows' => 10
            ))
            ->addValidator(new Zend_Validate_StringLength(0, 1000));
                
        $register = $this->createElement('submit','createProject');
        $register->setLabel('Create project')
                ->setIgnore(true);
                
        $this->addElements(array(                        
			$name,
			$enterprise,	
            $website,
			$startDate,
            $endDate,
			$note,
			$register,
			$idProject,
			$idPerson,
        ));
    }
}

